<?php

/**
 * Description of test
 *
 * @author Shang
 */
class zShang_Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    // Init namespace for modules
    protected function _initModules() {
        $front = $this->bootstrap("frontController")->frontController;
        $modules = $front->getControllerDirectory();
        set_include_path(get_include_path() . PATH_SEPARATOR . APPLICATION_PATH . '/modules/');

        $autoloader = Zend_Loader_Autoloader::getInstance();
        $controllers = array();
        foreach (array_keys($modules) as $module) {
            $directory = $front->getModuleDirectory($module);
            $controllers[strtolower($module)] = realpath($directory . '/controllers');
            $autoloader->pushAutoloader(new Zend_Application_Module_Autoloader(array(
                        'namespace' => ucwords($module),
                        'basePath' => $directory,
                        'resourceTypes' => array(
                            'model' => array(
                                'path' => 'models/',
                                'namespace' => 'Model',
                            )
                        )
                    )));
            $autoloader->registerNamespace($module . '_');
        }
        $front->setControllerDirectory($controllers);
    }

    /**
     * Ref: http://framework.zend.com/manual/1.12/en/zend.acl.html
     * Date: 17/12/2012
     */
    protected function _initAcl() {
        $acl = new Zend_Acl();

        $acl->addRole(new Zend_Acl_Role(Core_Role::ROLE_GUEST));
        $acl->addRole(new Zend_Acl_Role(Core_Role::ROLE_USER), Core_Role::ROLE_GUEST);

        $registry = Zend_Registry::getInstance();
        $registry->set('acl', $acl);
    }

    // Init bootstrap for modules
    protected function _initModuleBootstrap() {
        $front = Zend_Controller_Front::getInstance();
        $modules = $front->getControllerDirectory();

        foreach (array_keys($modules) as $module) {
            $directory = $front->getModuleDirectory($module);
            if (file_exists($directory . '/Bootstrap.php')) {
                $bootstrap_class = ucfirst($module) . '_Bootstrap';
                if (class_exists($bootstrap_class)) {
                    $bootstrap = new $bootstrap_class($this->getApplication());
                    $bootstrap->bootstrap();
                }
            }
        }
    }

    protected function _initDb() {
        $_dbOptions = $this->getOption('resources');
        $dbOptions = $_dbOptions['db'];

        // Setup database
        $db = Zend_Db::factory($dbOptions['adapter'], $dbOptions['params']);

        $db->setFetchMode(Zend_Db::FETCH_ASSOC);
        $db->query("SET NAMES 'utf8'");
        $db->query("SET CHARACTER SET 'utf8'");

        Zend_Registry::set('connectDB', $db);
        Zend_Db_Table::setDefaultAdapter($db);

        return $db;
    }

    protected function _initSession() {
        Zend_Session::start();
        $session = new Zend_Session_Namespace('zShang');
        $session->time = date('d-m-Y H:i');
        Zend_Registry::getInstance()->set('session', $session);
    }

    protected function _initConstant() {
        //TODO-ME
        define('STATIC_URL', '//' . $_SERVER['SERVER_NAME'] . '/football/public');
        define('IMAGE_URL', '//' . $_SERVER['SERVER_NAME'] . '/football/public/images');
        $frontConfig = $this->getOption('front');
        Zend_Registry::getInstance()->set('frontConfig', $frontConfig);
    }

    protected function _initTranslator() {

        $language = 'en';
        Zend_Registry::set('language', 'en_US');

        $translate = new Zend_Translate(array(
                    'adapter' => 'csv',
                    'content' => APPLICATION_PATH . '/languages/' . $language . '/main.csv',
                    'locale' => $language,
                    'delimiter' => ';'
                ));

        // add translation source of all modules
        $front = Zend_Controller_Front::getInstance();
        $modules = $front->getControllerDirectory();
        foreach (array_keys($modules) as $module) {
            $file = APPLICATION_PATH . '/modules/' . ucwords($module) . '/languages/' . $language . '/main.csv';
            if (file_exists($file)) {
                $translate->addTranslation(
                        array(
                            'adapter' => 'csv',
                            'content' => $file,
                            'locale' => $language,
                            'delimiter' => ';'
                        )
                );
            }
        }
        Zend_Registry::set('Zend_Translate', $translate);

        return $translate;
    }

}